import 'babel-polyfill'
import Vue from 'vue'
import App from './App'
import routes from './router'
import filter from './filter'
import VueRouter from 'vue-router'
// import Cropper from 'cropperjs'

import store from './vuex/store'

import axios from 'axios'
import qs from 'qs'
import Lockr from 'lockr'
import _ from 'lodash'

import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
//
import moment from 'moment'

import echarts from 'echarts'

import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

import _g from './assets/js/global'
import api from './assets/js/http'

import './assets/css/common.css'
import '../static/css/font-awesome.min.css'

import '../static/UE/ueditor.config.js'
import '../static/UE/ueditor.all.min.js'
import '../static/UE/lang/zh-cn/zh-cn.js'
import '../static/UE/ueditor.parse.min.js'

Vue.config.productionTip = false;

//axios.defaults.baseURL = 'http://47.100.127.123/voip-merchant/api/index.php/';
// axios.defaults.baseURL = 'http://localhost:20900/admin/';
axios.defaults.baseURL = 'http://test.credit-hq.com/myth/admin/';
// axios.defaults.baseURL = 'https://admin-api.jieqilai.cn/admin/';
axios.defaults.timeout = 1000 * 60;
//axios.defaults.headers.token = Lockr.get('token');
axios.defaults.headers.token = sessionStorage.token;
//axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded';
axios.defaults.headers['Content-Type'] = 'application/json;charset=UTF-8';

// var Cropper =

const router = new VueRouter({
    routes
});

router.beforeEach((to, from, next) => {
    NProgress.start();

    var t = sessionStorage.token;
    if(!t){
        localStorage.clear();
    }
    var token = Lockr.get('token');

    // if (!token && to.name && to.name.toLocaleLowerCase() != 'login') {
    //     next({
    //         path: '/login',
    //         query: {redirect: to.fullPath}
    //     })
    // }
    next()
});

router.afterEach(transition => {
    NProgress.done()
});

Vue.use(ElementUI);
Vue.use(VueRouter);
Vue.use(moment);

Vue.directive('permission', { // 注册一个全局自定义指令 v-permission
    bind (el, binding, vnode) {
        var permission = Lockr.get('permission');
        var flag = false;
        permission.forEach(function (val) {
            if (binding.value == val.permission) {
                flag = true;
                return;
            }
        });

        if (!flag) {
            el.style.display = 'none'
        }
    }
});

window.Cropper = require('cropperjs');
window.router = router;
window.store = store;
window.axios = axios;
window.Lockr = Lockr;
window.bus = new Vue();
window._ = _;
window._g = _g;
window.api = api;
window.moment = moment;
window.echarts = echarts;
new Vue({
    el: '#app',
    Cropper,
    router,
    store,
    filters: filter,
    template: '<App/>',
    components: {App}
}).$mount('#app');
